查看原文
其他

一行Python代码让图形秒变「手绘风」

The following article is from pythonic生物人 Author pythonic生物人

今天介绍一个手绘工具(matplotlib.pyplot.xkcd()),一行代码可将所有Matplotlib和Seaborn绘制的图形变为手绘风格

matplotlib.pyplot.xkcd()简介

这个Matplotlib子函数特别简单,只有三个参数,别看参数少,但功能可不小

matplotlib.pyplot.xkcd(scale=1#相对于不使用xkcd的风格图,褶皱的幅度
                       length=100#褶皱长度
                       randomness=2#褶皱的随机性
                      )

matplotlib.pyplot.xkcd()使用

如下,加with行代码即可,括号中参数按个人喜好决定是否设置~

with plt.xkcd(scale=1, length=100, randomness=2):
#with是临时使用一下,不影响其它图使用正常样式
    绘图代码
    。。。。。。
    plt.show()

matplotlib.pyplot.xkcd()使用实例

以下参考:Python可视化25|seaborn绘制矩阵图

#支持seaborn
import seaborn as sns
iris_sns = sns.load_dataset("iris")
with plt.xkcd():
    g = sns.pairplot(
        iris_sns,
        hue='species',  #按照三种花分类
        palette=['#dc2624''#2b4750''#45a0a2'])
    sns.set(style='whitegrid')
    g.fig.set_size_inches(1212)
    sns.set(style='whitegrid', font_scale=1.5)

以下参考:Python可视化29|matplotlib-饼图(pie)

import matplotlib.pyplot as plt
with plt.xkcd(
        scale=4,  #相对于不使用xkcd的风格图,褶皱的幅度
        length=120,  #褶皱长度
        randomness=2):  #褶皱的随机性
    plt.figure(dpi=150)
    patches, texts, autotexts = plt.pie(
        x=[123],  #返回三个对象
        labels=['A''B''C'],
        colors=['#dc2624''#2b4750''#45a0a2'],
        autopct='%.2f%%',
        explode=(0.100))
    texts[1].set_size('20')  #修改B的大小

    #matplotlib.patches.Wedge
    patches[0].set_alpha(0.3)  #A组分设置透明度
    patches[2].set_hatch('|')  #C组分添加网格线
    patches[1].set_hatch('x')

    plt.legend(
        patches,
        ['A''B''C'],  #添加图例
        title="Pie Learning",
        loc="center left",
        fontsize=15,
        bbox_to_anchor=(100.51))

    plt.title('Lovely pie', size=20)
    plt.show()
with plt.xkcd():
    from string import ascii_letters
    plt.figure(dpi=150)
    patches, texts, autotexts = plt.pie(
        x=range(112),
        labels=list(ascii_letters[26:])[0:11],
        colors=[
            '#dc2624''#2b4750''#45a0a2''#e87a59''#7dcaa9''#649E7D',
            '#dc8018''#C89F91''#6c6d6c''#4f6268''#c7cccf'
        ],
        autopct='%.2f%%',
    )
    plt.legend(
        patches,
        list(ascii_letters[26:])[0:11],  #添加图例
        title="Pie Learning",
        loc="center left",
        bbox_to_anchor=(100.51),
        ncol=2,  #控制图例中按照两列显示,默认为一列显示,
    )

以下参考:Python可视化|matplotlib12-垂直|水平|堆积条形图详解

import matplotlib.pyplot as plt
import numpy as np
with plt.xkcd():
    plt.figure(dpi=150)
    labels = ['Jack''Rose''Jimmy']
    year_2019 = np.arange(14)
    year_2020 = np.arange(14) + 1
    bar_width = 0.4

    plt.bar(
        np.arange(len(labels)) - bar_width / 2,  #为了两个柱子一样宽
        year_2019,
        color='#dc2624',
        width=bar_width,
        label='year_2019'  #图例
    )
    plt.bar(
        np.arange(len(labels)) + bar_width / 2,
        year_2020,
        color='#45a0a2',
        width=bar_width,
        label='year_2020'  #图例
    )
    plt.xticks(np.arange(03, step=1), labels, rotation=45)  #定义柱子名称
    plt.legend(loc=2)  #图例在左边

以下参考: Python可视化|matplotlib10-绘制散点图scatter

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
#数据准备
from sklearn import datasets

iris = datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(1501))),
                       columns=[
                           'sepal length(cm)''sepal width(cm)',
                           'petal length(cm)''petal width(cm)''class'
                       ])
with plt.xkcd():

    plt.figure(dpi=150)  #设置图的分辨率
    #plt.style.use('Solarize_Light2')  #使用Solarize_Light2风格绘图
    iris_type = pd_iris['class'].unique()  #根据class列将点分为三类
    iris_name = iris.target_names  #获取每一类的名称
    colors = ['#dc2624''#2b4750''#45a0a2']  #三种不同颜色
    markers = ['$\clubsuit, '.', '+']  #三种不同图形

    for i in range(len(iris_type)):
        plt.scatter(
            pd_iris.loc[pd_iris['
class'] == iris_type[i],
                        'sepal length(cm)'],  #传入数据x
            pd_iris.loc[pd_iris['class'] == iris_type[i],
                        'sepal width(cm)'],  #传入数据y
            s=50,  #散点图形(marker)的大小
            c=colors[i],  #marker颜色
            marker=markers[i],  #marker形状
            #marker=matplotlib.markers.MarkerStyle(marker = markers[i],fillstyle='full'),#设置marker的填充
            alpha=0.8,  #marker透明度,范围为0-1
            facecolors='r',  #marker的填充颜色,当上面c参数设置了颜色,优先c
            edgecolors='none',  #marker的边缘线色
            linewidths=1,  #marker边缘线宽度,edgecolors不设置时,该参数不起作用
            label=iris_name[i])  #后面图例的名称取自label

    plt.legend(loc='upper right')

参考: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xkcd.html#matplotlib.pyplot.xkcd


-END-


高质量推荐



对比Python这几种不同的编辑器/IDE优缺点,看看你最中意哪一款?


PyQuery解析网页用法入门讲解(含Python代码举例讲解+爬虫实战)


Python实现【实时】显示处理进度的6种形式(附源码和动图演示)



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存